const app = getApp(),url = app.globalData.url;
Page({
  data: {
    id:0,//熟练度id
    skill:[
      {id:3,'name':'一般'},
      {id:2,'name':'熟练'},
      {id:1,'name':'精通'},
    ],//熟练度
    xlindex:'',
    skillName:'',
  },

  onLoad: function (options) {
    console.log(options)
    var that = this;
    that.setData({ id:options.id });
    if(options.id > 0){
      wx.request({
        url: url + 'cvSkill/getCVSkillById',
        method: "GET",
        data:{id:options.id },
        success:function(res){
          console.log("技能详情",res);
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              skillName:ret.data.skillName,	//技能名称
              xlindex:ret.data.level,	//熟练程度
            })
            // var level = Number(ret.data.level)
            // for(let i in skill){
            //   if(skill[i].id == level){
            //     that.setData({
            //       xlindex:i,	//熟练程度
            //     })
            //     break;
            //   }
            // }
          }else{
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }else{
      return false
    }
  },
  //学校
  skillName(e){
    var that = this
    that.setData({skillName:e.detail.value})
  },
  
  //专业
  xinziChange: function (e) {
    var that = this;
    var val = e.detail.value//这个是  index
    var skill = that.data.skill
    for(let item in skill){
      if(item == val){
        // that.setData({level:skill[item].name,})
        that.setData({level:skill[item].id,})
        break;
      }
    }
    that.setData({xlindex:val,})//页面赋值 title:name
  },
  
  del(){
    var that = this
    var id = that.data.id
    wx.request({
      url: url + 'cvSkill/delete?id='+id,
      method: "DELETE",
      // data:{id:id },
      success:function(res){
        console.log("删除经历",res);
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'删除成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          
          that.setData({sub:true})
          wx.showToast({
            title:'删除失败',
            icon:'none',
          })
        }
      }
    })
  },
  
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(e){
    var that = this
    if(!this.data.skillName){
      wx.showToast({ 
        title: '请输入技能名称',
        icon: 'none'
      })
      return
    }
    if(!this.data.xlindex){
      wx.showToast({ 
        title: '请选择熟练程序',
        icon: 'none'
      })
      return
    }
    let sensWords = Object.values(this.sensWords || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
        icon:'none',
      })
			return false;
    }

   
    
    var xlid = that.data.id
    if(xlid > 0){
      //编辑
      var method = 'PUT'
      var api = 'cvSkill/updateSkill'
      var xlid = xlid
    }else{
      // 添加
      var method = 'POST'
      var api = 'cvSkill/add'
      var xlid = 0
    }
    var uid = wx.getStorageSync('user_massage')
    var data = {
      approval: '1',
      skillName:that.data.skillName,	//技能名称
      level:that.data.xlindex,	//熟练程度
      userUid:uid.userUid,	//用户编号
      id:xlid,//技能id
      createTime:'',
      updateTime:''
    }
    wx.request({
      url:url + api,
      header:{
      'context-type': 'application/json',
      },
      method: method,
      data:data,
      success:function(res){
        var ret = res.data
        if(ret.code == 200){
          wx.showToast({
            title:'保存成功',
            icon:'none',
          })
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
        }else{
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })
  
  //  wx.navigateTo({
  //        url:'/HomePage/linejianli/linejianli'
  //   })
  }
})